import { store } from "./store";
import { attr } from "./propery/index.js";
import { getAttrName } from "./propery/names";
function _func() {}

_func.prototype.setPage = function (page) {
  if (store.page.children) store.page.children.length = 0;
  store.page = page.clone();
  this.activate(store.page);
};

// 添加组件
_func.prototype.addCmpnt = function (xcmpnt) {
  if (!xcmpnt) return;
  let cmpnt = xcmpnt.clone();
  let actived = this.getActiveds(store.page.children);

  //查找父级是容器的节点
  let parent = null;
  for (let i = actived.parents.length - 1; i >= 0; i--) {
    let p = actived.parents[i];
    if (p._is_container) {
      parent = p;
      break;
    }
  }

  let container =
    actived.element && actived.element._is_container ? actived.element : parent;

  if (container && container._is_container) {
    if (container.push) container.push(cmpnt);
    else container.children.push(cmpnt);
  } else {
    store.page.children.push(cmpnt);
  }
  this.activate(cmpnt);
};

//  获取激活的组件和父级组件
_func.prototype.getActived = function (lst, parent = null) {
  let actived = { parent: null, element: null };
  lst.forEach((e) => {
    if (actived.element) return;
    if (e._active) {
      actived.element = e;
      actived.parent = parent;
      return;
    }
    if (e.children && e.children.length > 0)
      actived = this.getActived(e.children, e);
  });
  return actived;
};

//  获取激活的组件和多级父级组件
_func.prototype.getActiveds = function (lst, parents = []) {
  let actived = { parents: [], element: null };
  lst.forEach((e) => {
    if (actived.element) return;
    if (e._active) {
      actived.element = e;
      actived.parents = parents;
      return;
    }
    if (e.children && e.children.length > 0)
      actived = this.getActiveds(e.children, [...parents, e]);
  });
  return actived;
};
_func.prototype.find = function (id, lst = store.page.children, parents = []) {
  let actived = { parents: [], element: null };
  lst.forEach((e) => {
    if (actived.element) return;
    if (e._id == id) {
      actived.element = e;
      actived.parents = parents;
      return;
    }
    if (e.children && e.children.length > 0)
      actived = this.find(id, e.children, [...parents, e]);
  });
  return actived;
};

// 激活组件
_func.prototype.activate = function (cmpnt) {
  if (!cmpnt) return;

  //查找当前激活的组件，以及父组件，取消激活状态
  let activeds = this.getActiveds(store.page.children);
  if (activeds.parents) {
    activeds.parents.forEach((p) => {
      p._active = false;
      p._childActive = false;
    });
  }
  if (activeds.element) {
    activeds.element._active = false;
    activeds.element._childActive = false;
  }
  //修改当前组件的激活状态,以及父组件的激活状态
  cmpnt._active = true;
  cmpnt._childActive = true;
  activeds = this.getActiveds(store.page.children);
  if (activeds.parents) {
    activeds.parents.forEach((p) => {
      p._childActive = true;
    });
  }

  //暂存当前组件，并构建配置属性
  store.conf.selected = cmpnt;
};
// 取消激活
_func.prototype.unactive = function () {
  //取消当前激活的组件，以及父组件
  let actived = this.getActiveds(store.page.children);
  if (actived.element) {
    actived.element._active = false;
    actived.element._childActive = false;
  }
  if (actived.parents) {
    actived.parents.forEach((p) => {
      p._childActive = false;
      p._active = false;
    });
  }

  //激活根组件
  this.activate(store.page);
};

// 清空组件
_func.prototype.clear = function () {
  store.page.children.length = 0;
  this.activate(store.page);
};

// 删除组件
_func.prototype.remove = function (cmpnt) {
  if (!cmpnt) return;
  let actived = this.find(cmpnt._id, store.page.children);
  if (!actived.element) return;
  if (actived.element == store.page) return;
  if (actived.element._ctl.cantDrop) {
    window.$msg.error("不允许删除组件");
    return;
  }

  if (actived.parents && actived.parents.length > 0) {
    let current = actived.element;
    for (let i = actived.parents.length - 1; i >= 0; i--) {
      if (actived.parents[i].remove) {
        actived.parents[i].remove(current);
        if (actived.parents[i].children.length > 0) {
          break;
        }
      } else {
        let index = actived.parents[i].children.indexOf(current);
        actived.parents[i].children.splice(index, 1);
      }
      current = actived.parents[i];
    }
  } else {
    let index = store.page.children.indexOf(actived.element);
    store.page.children.splice(index, 1);
  }
};

// 移动到右边
_func.prototype.moveRight = function (cmpnt) {
  let actived = this.find(cmpnt._id, store.page.children);
  if (!actived.element) return;
  if (actived.parent) {
    let index = actived.parent.children.indexOf(actived.element);
    if (index >= actived.parent.children.length - 1) return;
    let ele = actived.parent.children.splice(index, 1)[0];
    actived.parent.children.splice(index + 1, 0, ele);
  } else {
    let index = store.page.children.indexOf(actived.element);
    if (index >= store.page.children.length - 1) return;
    let ele = store.page.children.splice(index, 1)[0];
    store.page.children.splice(index + 1, 0, ele);
  }
};

// 移动到左边
_func.prototype.moveLeft = function (cmpnt) {
  let actived = this.find(cmpnt._id, store.page.children);
  if (!actived.element) return;
  if (actived.parent) {
    let index = actived.parent.children.indexOf(actived.element);
    if (index <= 0) return;
    let ele = actived.parent.children.splice(index, 1)[0];
    actived.parent.children.splice(index - 1, 0, ele);
  } else {
    let index = store.page.children.indexOf(actived.element);
    if (index <= 0) return;
    let ele = store.page.children.splice(index, 1)[0];
    store.page.children.splice(index - 1, 0, ele);
  }
};

//获取指定对象的配置组件
_func.prototype.getAttrs = function (obj) {
  let attrs = {};
  if (!obj) return attrs;
  let objAttr = new attr(obj);

  Object.keys(obj).forEach((key) => {
    if (key.startsWith("_")) return;
    if (
      _.isFunction(obj[key]) ||
      _.isPlainObject(obj[key]) ||
      _.isNil(obj[key]) ||
      (_.isArray(obj[key]) && (obj[key].length == 0 || _.isObject(obj[key][0])))
    )
      return;

    let eles = objAttr.getEles(key);
    eles.forEach((ele) => {
      if (!attrs[key]) {
        attrs[key] = {
          label: getAttrName(key),
          name: key,
          data: obj,
        };
      }
      ele.valueKey = key;
      attrs[key].cmpnt = ele;
    });
  });
  return attrs;
};
const func = new _func();
export { func };
